package team.hyznrj.studentsys.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.annotations.Api;
import team.hyznrj.studentsys.dto.ResultDto;
import team.hyznrj.studentsys.entity.DataReport;
import team.hyznrj.studentsys.service.ExcelService;
import team.hyznrj.studentsys.service.FileService;
import team.hyznrj.studentsys.utils.KeyUtil;
import team.hyznrj.studentsys.utils.ResultUtil;
import team.hyznrj.studentsys.utils.ValueUtil;

/**
 * @author ReMidDream
 * @date 2018-04-11 20:15
 **/
@RestController
@RequestMapping("/file")
@Api(value = "file")
public class FileController {
	private static final String XLS = ".xls";
	@Value("${StudentSavePath}")
	private String savePath;
	@Autowired
	private FileService fileService;
	@Autowired
	private ExcelService excelService;
	/**
	 * xlse Type :
	 * application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xls
	 * Type ; application/vnd.ms-excel docx Type :
	 * application/vnd.openxmlformats-officedocument.wordprocessingml.document
	 * doc Type : application/msword
	 * 
	 * @param files
	 * @return
	 */
	@PostMapping(value = "/upload")
	public ResultDto<?> fileUpload(MultipartFile[] files) {
		System.out.println("文件数量:" + files.length);
		return ResultUtil.Success();
	}

	@RequestMapping("/download/quality")
	public ResponseEntity<byte[]> downloadQuality() throws Exception {
//		File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "module/quality.xlsx");
		return fileService.fileDownload(savePath + "/module/quality.xlsx", "操行分导入模版表.xlsx");
	}
	@RequestMapping("/download/course")
	public ResponseEntity<byte[]> downloadCourse() throws Exception {
		return fileService.fileDownload(savePath + "/module/course.xls", "成绩导入模版表.xlsx");
	}

	@RequestMapping("/download/studentMessage")
	public ResponseEntity<byte[]> download(HttpServletRequest request) throws Exception {
		//TODO
		String flag=(String)request.getAttribute("flag");
		String path=savePath + "/module/student/"+KeyUtil.getUniqueKey()+".xls";
		excelService.WriteExcelInStudentMessageDyn(path, flag);
		return fileService.fileDownload(path, "学生信息模版表.xls");
	}

	/**
	 * 下载动态文件（保存路径固定）
	 * 
	 * @return
	 * @throws Exception
	 */
	// 9icNckVUx%2FTKE5bRh9Qv07YNOwDUU2gixgQiXnPfOtY%3D
	@RequestMapping("/download/path/{url}")
	public ResponseEntity<byte[]> downDynFile(@PathVariable(value = "url") String path, HttpServletRequest request)
			throws Exception {
		String filePath = savePath + ValueUtil.encodeUrl(path, (String)request.getAttribute("flag"));
		return fileService.fileDownload(filePath, path + XLS);
	}
}
